/**
 * Created by
 * @Author  Administrator
 * @Date    2016/8/30
 */

/** @Author Credo
    页面布局左侧菜单
 */
SexyAidsBackend.controller('indexCtrl', ['$scope', function ($scope) {
    $scope.menus = [
        {
            pTitle: '内容',
            icon: 'g_translate',
            styles: {
                "display": "none"
            },
            cItem: [
                {
                    itemId: 1,
                    title: '首页内容管理',
                    routerUrl: 'content'
                }
            ]
        },
        {
            pTitle: '类别',
            icon: 'view_list',
            styles: {
                "display": "none"
            },
            cItem: [
                {
                    itemId: 2,
                    title: '详情内容',
                    routerUrl: 'content'
                },
                {
                    itemId: 3,
                    title: '所属产品',
                    routerUrl: 'content'
                }
            ]
        },
        {
            pTitle: '产品',
            icon: 'shopping_cart',
            styles: {
                "display": "none"
            },
            cItem: [
                {
                    itemId: 4,
                    title: '内容管理',
                    routerUrl: 'product'
                },
                {
                    itemId: 5,
                    title: '图片管理',
                    routerUrl: 'content'
                },
                {
                    itemId: 6,
                    title: '属性管理',
                    routerUrl: 'content'
                },
                {
                    itemId: 7,
                    title: '类型管理',
                    routerUrl: 'content'
                }
            ]
        },
        {
            pTitle: '会员',
            icon: 'face',
            styles: {
                "display": "none"
            },
            cItem: [
                {
                    itemId: 8,
                    title: '信息',
                    routerUrl: 'content'
                },
                {
                    itemId: 9,
                    title: '分组',
                    routerUrl: 'content'
                },
                {
                    itemId: 10,
                    title: '数据',
                    routerUrl: 'content'
                }
            ]
        },
        {
            pTitle: '基础数据',
            icon: 'verified_user',
            styles: {
                "display": "none"
            },
            cItem: [
                {
                    itemId: 11,
                    title: '网站信息',
                    routerUrl: 'content'
                },
                {
                    itemId: 12,
                    title: '数据字典',
                    routerUrl: 'dictionary'
                }
            ]
        },
        {
            pTitle: '系统设置',
            icon: 'settings',
            styles: {
                "display": "block"
            },
            cItem: [
                {
                    itemId: 13,
                    title: '用户管理',
                    routerUrl: 'user'
                },
                {
                    itemId: 14,
                    title: '角色管理',
                    routerUrl: 'role'
                },
                {
                    itemId: 15,
                    title: '操作管理',
                    routerUrl: 'content'
                },
                {
                    itemId: 16,
                    title: '权限管理',
                    routerUrl: 'content'
                }
            ]
        }
    ]

    // case 1： 点击时清空所有数组， 当前索引的设置为active
    // case 2： 点击传递当前元素ID， 对比元素自身的id， 需为每个元素配置唯一标识
    $scope.isCurrent = function(index){
        $scope.currentIndex = index
    }

}])


/** @Author Credo
    user用户管理列表
 */
SexyAidsBackend.controller('userCtrl', ['$scope', function ($scope) {

    $scope.page = {
        currentPage: 1,
        customPage: '',
        totalPage : 2,
        loadingState : false
    }

    $scope.userLists = [
        {
            'uid': 102,
            'account': 'ljs1990126',
            'username': '风接着吹',
            'role': '超级管理员',
            'right': ['查看', '添加', '删除', '修改']
        }, {
            'uid': 101,
            'account': 'justbamboo',
            'username': '太阳永远不懂向日葵的执着',
            'role': '管理员',
            'right': ['查看','修改']
        }, {
            'uid': 100,
            'account': 'lwchuhao',
            'username': '请给我个理由忘记你',
            'role': '普通用户',
            'right': ['查看']
        }
    ]

    $scope.testNext = [
        {
            'uid': 110,
            'account': 'Test Next',
            'username': 'I am Next',
            'role': '超级管理员',
            'right': ['查看', '添加', '删除', '修改']
        }
    ]

    // 分页功能
    $scope.firstPage = function () {
        if( $scope.page.currentPage == 1 ){
            alert('当前已是第一页')
            return false
        }else{
            $scope.page.currentPage = 1
        }
    }

    $scope.prevPage = function () {
        // $scope.page.loadingState = true
        if ( $scope.page.currentPage>1 ) {
            $scope.page.currentPage -= 1
            $scope.userLists = [
                {
                    'uid': 102,
                    'account': 'ljs1990126',
                    'username': '风接着吹',
                    'role': '超级管理员',
                    'right': ['查看', '添加', '删除', '修改']
                }, {
                    'uid': 101,
                    'account': 'justbamboo',
                    'username': '太阳永远不懂向日葵的执着',
                    'role': '管理员',
                    'right': ['查看','修改']
                }, {
                    'uid': 100,
                    'account': 'lwchuhao',
                    'username': '请给我个理由忘记你',
                    'role': '普通用户',
                    'right': ['查看']
                }
            ]
        } else {
            alert('没有上一页')
            return false
        }  
    }

    $scope.nextPage = function () {
        // $scope.page.loadingState = true
        if ( $scope.page.currentPage<$scope.page.totalPage ) {
            $scope.page.currentPage += 1
            $scope.userLists = $scope.testNext
        } else {
            alert('没有下一页')
        }
    }

    $scope.lastPage = function () {
        if( $scope.page.currentPage == $scope.page.totalPage ){
            alert('当前已是最后一页')
            return false
        }else{
            $scope.page.currentPage = $scope.page.totalPage
        }  
    }

    $scope.goPage = function ($event) {
        if($event.keyCode==13 && !isNaN($scope.page.customPage)){
            $scope.page.currentPage = parseInt($scope.page.customPage)
            console.log('go to page'+ $scope.page.currentPage)
            $scope.page.customPage = ''
            $scope.clearStyle = false
            $('.mdl-textfield').removeClass('is-dirty is-focused')
        }
    }

    $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
        componentHandler.upgradeAllRegistered()
    })
}])


/** @Author Credo
    userEdit用户编辑    
 */
SexyAidsBackend.controller('userEditCtrl', ['$scope','$stateParams', function($scope,$stateParams){
    $scope.roleId = []
    $scope.submitted = false    //  用来提交时判断表单域(为了在点击按钮提交后再进行valid)

    $scope.userNew = {
        username: '',
        password: '',
        compwd: '',
        name: '',
        sexy: null,
        summary: '',
        role: '',
        headImg: '/avBackend/src/images/test.jpg'
    }

    $scope.userInfo = {
        username: 'ljs1990126',
        password: '123456',
        compwd: '123456',
        name: '风接着吹',
        sexy: 2,
        summary: '像风一样的男人',
        role: '角色3',
        headImg: '/avBackend/src/images/test.jpg'
    }

    $scope.treeSource = [
        { value: 10, icon: '/avBackend/src/images/administrator.png', label: '角色1' },
        { value: 11, icon: '/avBackend/src/images/administrator.png', label: '角色2' },
        { value: 20, icon: '/avBackend/src/images/administrator.png', label: '角色3' },
        { value: 21, icon: '/avBackend/src/images/administrator.png', label: '角色4' },
        { value: 30, icon: '/avBackend/src/images/administrator.png', label: '角色5' }
    ]

    $scope.treeEdit = [
        { value: 10, icon: '/avBackend/src/images/administrator.png', label: '角色1' },
        { value: 11, icon: '/avBackend/src/images/administrator.png', label: '角色2' },
        { value: 20, icon: '/avBackend/src/images/administrator.png', label: '角色3', checked:true },
        { value: 21, icon: '/avBackend/src/images/administrator.png', label: '角色4' },
        { value: 30, icon: '/avBackend/src/images/administrator.png', label: '角色5' }
    ]

    $scope.addUser = function (isValid) {
        if (isValid) {
            var requestData = {
                user_info: {
                    uid: '',
                    username: $scope.userNew.username,
                    password: $scope.userNew.password,
                    role_id: $scope.roleId
                },
                account_info: {
                    name: $scope.userNew.name,
                    sex: $scope.userNew.sexy,
                    head_img_url: $scope.userNew.headImg,
                    account_desc: $scope.userNew.summary
                }
            }
            console.log(requestData)
        }else{
            $scope.submitted = true
            alert('未正确填写！')
        }
    }
    
    $scope.save = function(isValid) {
        if (isValid) {
            var requestData = {
                user_info: {
                    uid: $stateParams.uid,
                    username: $scope.userInfo.username,
                    password: $scope.userInfo.password,
                    role_id: $scope.roleId
                },
                account_info: {
                    name: $scope.userInfo.name,
                    sex: $scope.userInfo.sexy,
                    head_img_url: $scope.userInfo.headImg,
                    account_desc: $scope.userInfo.summary
                }
            }
            console.log(requestData)
        }else{
            $scope.submitted = true
            alert('未正确填写！')
        }
    }
}])